import request from '@/utils/request'
import { BaseResponse, PageParams, PageResult } from '@/types/api'

// API接口相关类型定义
export interface ApiInfo {
  id: number;
  path: string;
  category: string;
  desc: string;
  method: string;
  creator?: string;
  createdAt?: string;
  updatedAt?: string;
}

export interface ApiListParams extends PageParams {
  path?: string;
  category?: string;
  method?: string;
  creator?: string;
}

export interface ApiData {
  path: string;
  category: string;
  desc: string;
  method: string;
}

export interface ApiTree {
  [category: string]: ApiInfo[];
}

// 获取接口列表
export function getApis(params: ApiListParams): Promise<BaseResponse<PageResult<ApiInfo>>> {
  return request({
    url: '/api/list',
    method: 'get',
    params
  })
}

// 获取接口树(按接口Category字段分类)
export function getApiTree(params?: ApiListParams): Promise<BaseResponse<ApiTree>> {
  return request({
    url: '/api/tree',
    method: 'get',
    params
  })
}

// 创建接口
export function createApi(data: ApiData): Promise<BaseResponse> {
  return request({
    url: '/api/create',
    method: 'post',
    data
  })
}

// 更新接口
export function updateApiById(apiId: number, data: Partial<ApiData>): Promise<BaseResponse> {
  return request({
    url: '/api/update/' + apiId,
    method: 'patch',
    data
  })
}

// 批量删除接口
export function batchDeleteApiByIds(data: { apiIds: number[] }): Promise<BaseResponse> {
  return request({
    url: '/api/delete/batch',
    method: 'delete',
    data
  })
}
